package annotation.demo05;

import annotation.demo05.adapter.TypeChange;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.Properties;

public class PersonFactory {
    public static Person getPerson(){
        Class<Person> personClass = Person.class;
        Person person = null;
        try {
            person = personClass.newInstance();
            Field[] fields = personClass.getDeclaredFields();
            //读取配置文件
            Properties properties = new Properties();
            InputStream is = PersonFactory.class.getClassLoader().getResourceAsStream("person.properties");
            System.out.println(is);
            properties.load(is);
            for(Field field:fields){
                field.setAccessible(true);
                String property = properties.getProperty(field.getName());
                field.set(person, TypeChange.typeChange(property,field.getClass()));
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return person;
    }
}
